package com.sl.au.service;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.sl.au.entity.JPushMsg;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
@Service("jgPushService")
public class JGPushService {
	 // 日志  
    private static final Logger log = Logger.getLogger(JGPushService.class);  
 
	
	private  final  String apiKey = "a23b1868692064298e50cc64";
	private  final  String secretKey = "7a9b35b13075e2147c2df4a3";
	 private  JPushClient jPushClient = new JPushClient(secretKey,apiKey);
    
	 @Async
	 public  void sendClient(JPushMsg pushData,  
	            Map<String, String> extraMap) {  
	        PushPayload payload = buildPushObject_all_alias_alert(pushData,  
	                extraMap);  
	        try {  
	            PushResult result = jPushClient.sendPush(payload);  
	        
	        } catch (Exception e) {  
	            log.error("Connection error, should retry later", e);  
	        } 
	    }  
	  
	    public  PushPayload buildPushObject_all_alias_alert(  
	            JPushMsg pushData, Map<String, String> extraMap) {  
	        Message message ;
	        if(null!=extraMap){
	        	message= Message.newBuilder().setMsgContent(pushData.getContent())  
	        			.addExtras(extraMap)
		                .setTitle(pushData.getTitle()).setContentType(pushData.getTag()).build();  
	        }else
	        {
	        	message= Message.newBuilder().setMsgContent(pushData.getContent())  
		                .setTitle(pushData.getTitle()).setContentType(pushData.getTag()).build();  
	        }
	        		
	        return PushPayload.newBuilder().setPlatform(Platform.all())  
	                .setAudience(Audience.alias(pushData.getAlias()))  
              .setNotification(Notification.alert(pushData.getContent()))//通知  
	                .setMessage(message)//使用自定义消息推送  
	                .build();  
	    }

	
	  
	

}
